package wyp.library.ui.listview.drag;

import wyp.library.R;

import wyp.library.ui.listview.drag.DrapHeadDelegate;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class HeadView_Example extends LinearLayout implements DrapHeadDelegate {
 
	// 普通状态 setNormalState 下拉状态  setPullRefreshState  松开刷新状态 setReleaseRefreshState 加载状态 setLoadingState
	public ImageView arrowImageView;// 下拉图标（mHeadView）
	public TextView refreshTextView; // 刷新msg（mHeadView）
	public ProgressBar progressBar;// 刷新进度体（mHeadView）
	public TextView lastUpdateTextView;// 更新事件（mHeadView）
	private Animation animation, reverseAnimation;// 旋转动画，旋转动画之后旋转动画.

	public HeadView_Example(Context context) {
		super(context);
		LayoutInflater.from(context).inflate(R.layout.example_head, this, true);
		this.arrowImageView = (ImageView) findViewById(R.id.head_arrowImageView);
		this.arrowImageView.setMinimumWidth(60);
		this.refreshTextView = (TextView) findViewById(R.id.head_tipsTextView);
		this.progressBar = (ProgressBar) findViewById(R.id.head_progressBar);
		initAnimation();
	}

	/***
	 * 初始化动画
	 */
	private void initAnimation() {
		// 旋转动画
		animation = new RotateAnimation(0, -180, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
		animation.setInterpolator(new LinearInterpolator());// 匀速
		animation.setDuration(150);
		animation.setFillAfter(true);// 停留在最后状态.
		// 反向旋转动画
		reverseAnimation = new RotateAnimation(-180, 0, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
		reverseAnimation.setInterpolator(new LinearInterpolator());
		reverseAnimation.setDuration(150);
		reverseAnimation.setFillAfter(true);
	}

	@Override
	public View getView() {
		return this;
	}
     /*
      *   普通状态
      */
	@Override
	public void setNormalState() {
		this.arrowImageView.clearAnimation();// 清除动画
		this.arrowImageView.setImageResource(R.drawable.wyp_arrow);
	}
     //下拉状态
	@Override
	public void setPullRefreshState() {
		this.progressBar.setVisibility(View.GONE);// 隐藏进度条
		this.arrowImageView.setVisibility(View.VISIBLE);// 下拉图标
		this.refreshTextView.setText("下拉可以刷新");
		this.arrowImageView.clearAnimation();// 清除动画

		// 是有可刷新状态（LV_RELEASE_REFRESH）转为这个状态才执行，其实就是你下拉后在上拉会执行.
		// if (_back) {
		// _back = false;
		// this.arrowImageView.clearAnimation();// 清除动画
		// this.arrowImageView.startAnimation(reverseAnimation);// 启动反转动画
		// }
	}
    
	//松开刷新状态
	@Override
	public void setReleaseRefreshState() {
		this.progressBar.setVisibility(View.GONE);// 隐藏进度条
		this.arrowImageView.setVisibility(View.VISIBLE);// 显示下拉图标
		this.refreshTextView.setText("松开获取更多");
		this.arrowImageView.clearAnimation();// 清除动画
		this.arrowImageView.startAnimation(animation);// 启动动画
	}
    //加载状态
	@Override
	public void setLoadingState() {
		this.progressBar.setVisibility(View.VISIBLE);
		this.arrowImageView.clearAnimation();
		this.arrowImageView.setVisibility(View.GONE);
		this.refreshTextView.setText("载入中...");
	}

}
